﻿using IronLua.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IronLua.Runtime
{
    internal class CallFrame
    {

        private const int hostFrameIndex = 0;

        public static readonly CallFrame Host =
            new CallFrame(proto: null,
                programCounter: 0, 
                baseStackIndex: CallFrame.hostFrameIndex, 
                returnStackIndex: 0, 
                expectedResultCount: VirtualMachine.MultipleResults);

        public CallFrame(Prototype proto, int programCounter, int baseStackIndex, int returnStackIndex, int expectedResultCount)
        {
            this.Prototype = proto;
            this.ProgramCounter = programCounter;
            this.BaseStackIndex = baseStackIndex;
            this.ReturnStackIndex = returnStackIndex;
            this.WantedResults = expectedResultCount;
        }

        public readonly Prototype   Prototype;
        public int                  BaseStackIndex;
        public int                  ProgramCounter;
        public readonly int         ReturnStackIndex;
        public readonly int         WantedResults;

        public override string ToString()
        {
            var name = this.Prototype != null ? this.Prototype.Name : "?";
            var kind = this.Prototype != null
                           ? this.Prototype.Kind == PrototypeKind.Bytecode
                                 ? "Lua"
                                 : this.Prototype.Kind == PrototypeKind.IL
                                       ? "CLR"
                                       : "?"
                           : "?";

            return "{0} @ {1} ({2})".FormatWith(name, this.BaseStackIndex, kind);
        }
    }

}
